/**
 * Created by Kylin on 2016/12/6.
 */
import {combineReducers} from 'redux';
import {ADD_TODO, COMPLETED_TODO, SET_VISIBILITY_FILTER, VisibilityFilters, SHOW} from './actions';

const {SHOW_ALL} = VisibilityFilters;

function todos(state = [], action) {
    switch (action.type) {
        case ADD_TODO:
            return [...state, {text: action.text, completed: false}];
        case COMPLETED_TODO:
            return [
                ...state.slice(0, action.index),
                Object.assign({}, state[action.index], {completed: true}),
                ...state.slice(action.index + 1)
            ];
        default:
            return state
    }
}

function visibilityFilter(state = SHOW_ALL, action) {
    switch (action.type) {
        case SET_VISIBILITY_FILTER:
            return action.filter;
        default:
            return state
    }
}

function showButton(state = true, action) {
    if (action.text == false) {
        state = action.text;
    }
    switch (action.type) {
        case SHOW:
            return state;
        default:
            return state
    }
}

export default combineReducers({
    todos,
    visibilityFilter,
    showButton
})